package com.app.micaihu.bean.news;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.micaihu.R;
import com.app.micaihu.custom.view.CustomImageView;
import com.app.micaihu.listener.AutoWidthControllerListener;
import com.app.micaihu.utils.ImageLoadUtils;
import com.app.micaihu.view.user.userinfo.activity.MyHomepageDynamicActivity;
import com.app.utils.util.ViewSizeUtils;
import com.app.utils.util.image.LoadImageUtils;
import com.app.utils.util.view.expression.view.BiaoQinTextView;
import com.blankj.utilcode.util.StringUtils;


/**
 * 小图话题
 * Created by linkai on 2017/3/17.
 */
public class ViewNewsTopicSmall implements View.OnClickListener {


    public CustomImageView userhead;//作者头像
    public TextView username;//用户名字
    public TextView rankName;//军衔名称
    public CustomImageView rankIcon;//军衔图片
    public BiaoQinTextView title;//标题
    public CustomImageView smallImgLeft;//一张左大图
    public CustomImageView smallImgRight;//一张右大图
    public CustomImageView imgTopic;//话题图片
    private TextView commentView;
    private TextView zanView;
    private ImageView ivHonourPic;
    private TextView tvArmyGroupInfo;
    //推广类型小标的加载监听
    AutoWidthControllerListener autoWidthControllerListener;

    public ViewNewsTopicSmall(View convertView) {
        initView(convertView);
    }


    /**
     * 初始化控件
     *
     * @param convertView
     */
    public void initView(View convertView) {

        this.title = (BiaoQinTextView) convertView.findViewById(R.id.item_public_title);
        this.userhead = (CustomImageView) convertView.findViewById(R.id.item_public_userhead);
        this.username = (TextView) convertView.findViewById(R.id.tv_nickname);
        this.rankIcon = (CustomImageView) convertView.findViewById(R.id.civ_rank);
        this.rankName = (TextView) convertView.findViewById(R.id.tv_rankname);
        this.commentView = (TextView) convertView.findViewById(R.id.tv_commentnum);
        this.smallImgLeft = (CustomImageView) convertView.findViewById(R.id.item_small_image_left);
        this.smallImgRight = (CustomImageView) convertView.findViewById(R.id.item_small_image_right);
        this.imgTopic = (CustomImageView) convertView.findViewById(R.id.item_public_alt_mark);
        this.zanView = (TextView) convertView.findViewById(R.id.tv_good);
        this.ivHonourPic = convertView.findViewById(R.id.ivHonourPic);
        this.tvArmyGroupInfo = convertView.findViewById(R.id.tvArmyGroupInfo);
        ViewSizeUtils.changeViewAuto1080(this.smallImgLeft, 324, 232);
        ViewSizeUtils.changeViewAuto1080(this.smallImgRight, 324, 232);
        autoWidthControllerListener = new AutoWidthControllerListener(this.imgTopic);
        this.userhead.setOnClickListener(this);
        this.username.setOnClickListener(this);
        convertView.setTag(this);
    }

    /**
     * 加载数据
     *
     * @param newsEntity
     */
    public void loadData(NewsEntity newsEntity) {
        if (StringUtils.isEmpty(newsEntity.getHonourPic())) {
            this.ivHonourPic.setVisibility(View.GONE);
        } else {
            this.ivHonourPic.setVisibility(View.VISIBLE);
            ImageLoadUtils.loadImage(newsEntity.getHonourPic(), this.ivHonourPic);
        }
        this.tvArmyGroupInfo.setText(newsEntity.getArmyGroupInfo());
        LoadImageUtils.getInstance().loadWifiImage(this.userhead, newsEntity.getAuthorHeadPic());
        LoadImageUtils.getInstance().loadWifiImage(this.rankIcon, newsEntity.getAuthorRankIcon());
        this.rankName.setText(newsEntity.getAuthorRankName());
        this.username.setText(newsEntity.getAuthorName());
        this.username.setTag(newsEntity.getAuthorId());
        this.userhead.setTag(newsEntity.getAuthorId());
        if (!TextUtils.isEmpty(newsEntity.getArticleTitle())) {
            if (this.title.getVisibility() == View.GONE) {
                this.title.setVisibility(View.VISIBLE);
            }
            this.title.setPicText(newsEntity.getArticleTitle());
            if (newsEntity.getArticleThumb() == null || newsEntity.getArticleThumb().length == 0) {
                if (smallImgRight.getVisibility() == View.VISIBLE) {
                    smallImgRight.setVisibility(View.GONE);
                    imgTopic.setVisibility(View.GONE);
                }
                if (smallImgLeft.getVisibility() == View.VISIBLE) {
                    smallImgLeft.setVisibility(View.GONE);
                }
            } else {
                if (smallImgRight.getVisibility() == View.GONE) {
                    smallImgRight.setVisibility(View.VISIBLE);
                    imgTopic.setVisibility(View.VISIBLE);
                }
                LoadImageUtils.getInstance().loadImage(imgTopic, newsEntity.getArticleMark(), autoWidthControllerListener);
                if (smallImgLeft.getVisibility() == View.VISIBLE) {
                    smallImgLeft.setVisibility(View.GONE);
                }
                LoadImageUtils.getInstance().loadWifiImage(this.smallImgRight, newsEntity.getArticleThumb()[0]);
            }
        } else {
            this.title.setVisibility(View.GONE);
            if (newsEntity.getArticleThumb() == null || newsEntity.getArticleThumb().length == 0) {
                if (smallImgRight.getVisibility() == View.VISIBLE) {
                    smallImgRight.setVisibility(View.GONE);
                    imgTopic.setVisibility(View.GONE);
                }
                if (smallImgLeft.getVisibility() == View.VISIBLE) {
                    smallImgLeft.setVisibility(View.GONE);
                }
            } else {
                if (smallImgRight.getVisibility() == View.VISIBLE) {
                    smallImgRight.setVisibility(View.GONE);
                    imgTopic.setVisibility(View.GONE);
                }
                if (smallImgLeft.getVisibility() == View.GONE) {
                    smallImgLeft.setVisibility(View.VISIBLE);
                }
                LoadImageUtils.getInstance().loadWifiImage(this.smallImgLeft, newsEntity.getArticleThumb()[0]);
            }
        }

        this.commentView.setText(newsEntity.getCommentNum() + "评论");
        this.zanView.setText(newsEntity.getPraiseNum());
    }


    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch (v.getId()) {
            case R.id.item_public_userhead:
            case R.id.tv_nickname:
                //进入个人主页
                try {
                    String uid = v.getTag().toString();
                    MyHomepageDynamicActivity.goToUserHomePage((Activity) v.getContext(), uid);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

}
